package com.guimin.wuye.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guimin.user.dto.LoginInfoDto;
import com.guimin.user.entity.UserInfo;
import com.guimin.user.service.UserInfoService;
import com.guimin.wuye.dao.GmPropertyDao;
import com.guimin.wuye.dto.AddPropertyDto;
import com.guimin.wuye.entity.GmProperty;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;

/**
 * 物业表(GmProperty)表服务实现类
 *
 * @author hzy
 * @since 2021-11-25 08:57:39
 */
@Service("gmPropertyService")
public class GmPropertyService extends ServiceImpl<GmPropertyDao, GmProperty> {
    /**
     * 服务对象
     */
    @Resource
    private GmPropertyDao gmPropertyDao;
    @Resource
    private UserInfoService userInfoService;

    /**
     * 添加物业
     * @param dto
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public boolean addProperty(AddPropertyDto dto) {
        //生成物业账户
        if (userInfoService.count(new LambdaQueryWrapper<UserInfo>().eq(UserInfo::getPhone, dto.getPhone())) == 0) {
            if (null == userInfoService.register(new LoginInfoDto().setPhone(dto.getPhone()).setRole("PROPERTY"))) {
                return false;
            }
        }
        return super.save(new GmProperty()
                .setName(dto.getName())
                .setAdminName(dto.getAdminName())
                .setPhone(dto.getPhone())
                .setCreateDatetime(new Date())
                .setState("1"));
    }
}

